
function Dump(data)
{
    Verbose("Dumping Data:");
    
    for (iData = 0; iData < data.length; iData++)
    {
        if ((iData == 0) || (0 == (iData % 8)))
            TextHistory.value += ((0 == iData) ? "" : "\n") + "   ";
            
        TextHistory.value += "[" + data[iData] + "]";

        if (64 < iData)
        {
            TextHistory.value += "...";
            break;
        }
    }
    
    Verbose("\n");
}
 
function EncryptDecryptDemo(dog)
{
    if (window.ActiveXObject || "ActiveXObject" in window)  //IE
    {
        EncryptDecryptDemo_IE(dog);
    }
    else
    {
        EncryptDecryptDemo_NP(dog);
    }
}

function EncryptDecryptDemo_IE(dog)
{
    if (!dog)
        return;
        
    Verbose("Encrypt/Decrypt Demo");
    Verbose("¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯");
    

    Verbose("--The hard way");
    Verbose("¯¯¯¯¯¯¯¯¯¯¯¯¯¯");

    var data = new String("SuperDog is great");
    Verbose("Encrypting: \"" + data + "\"");
    
    var coll = dogApplication.Collection;
    if (!coll)
        return;
        
    for (items = 0; items < data.length; items++)
    {
        var item = data.charCodeAt(items);
        coll.Add(item);
    }
    
    var status = dog.Encrypt(coll);
    ReportStatus(status);
    if (0 != status)
    {
        Verbose("");
        return;
    }
    
    Verbose("Encrypted Data:");
    TextHistory.value += "   ";
    
    for (collItem = 0; collItem < coll.Count; collItem++)
    {
        var item = coll.Item(collItem);
        TextHistory.value += item + " ";
    }
    
    Verbose("");
    Verbose("");
    Verbose("Decrypting Data");
    
    status = dog.Decrypt(coll);
    ReportStatus(status);
    if (0 != status)
    {
        Verbose("");
        return;
    }
        
    data = "";
    for (collItem = 0; collItem < coll.Count; collItem++)
    {
        data += String.fromCharCode(coll.Item(collItem));
    }
    
    Verbose("Decrypted Data: \"" + data + "\"");
    Verbose("");

    Verbose("--Encrypt/Decrypt String");
    Verbose("¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯");

    data = new String("SuperDog is great");
    Verbose("Encrypting String: \"" + data + "\"");
    
    var res = dog.EncryptString(data);
    ReportStatus(res.status);
    if (0 != res.status)
    {
        Verbose("");
        return;
    }
    
    Verbose("Encrypted String: \'" + res.string + "\'");
    Verbose("");

    data = res.string;
    Verbose("Decrypting String \'" + data + "\'");
    
    res = dog.DecryptString(data);
    ReportStatus(res.status);
    if (0 != res.status)
    {
        Verbose("");
        return;
    }
        
    Verbose("Decrypted String: \"" + res.string + "\"");
    Verbose("");
    
    Verbose("--Direct Array Access");
    Verbose("¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯");

    coll.Clear();

    Verbose("Encrypting");

    for (items = 0; items <= 10; items++)
    {
        coll.Add(items * 2);
    }

    status = dog.Encrypt(coll);
    ReportStatus(status);
    if (0 != status)
    {
        Verbose("");
        return;
    }
    
    Verbose("Encrypted Data:");

    // Fake one item to create a Variant VB Array
    coll.Add(0.1);

    var rVB = new VBArray(coll.Items);
    var rJS = rVB.toArray();
    Dump(rJS); 
    
    // Remove faked item
    coll.Remove(rVB.ubound(1));
        
    Verbose("Decrypting Data");
    
    status = dog.Decrypt(coll);
    ReportStatus(status);
    if (0 != status)
    {
        Verbose("");
        return;
    }
        
    Verbose("Decrypted Data:");

    // Fake one item to create a Variant VB Array
    coll.Add(0.1);

    rVB = new VBArray(coll.Items);
    rJS = rVB.toArray();
    Dump(rJS); 
    
    // Remove faked item
    coll.Remove(rVB.ubound(1));
    Verbose("");
}

function EncryptDecryptDemo_NP(dog)
{
    if (!dog)
        return;
        
    Verbose("Encrypt/Decrypt Demo");
    Verbose("¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯");

    var data = "SuperDog is great";
    Verbose("Encrypting String: \"" + data + "\"");
    
    var status = dogApplication.EncryptString(dog, data);
    ReportStatus(status);
    if (0 != status)
    {
        Verbose("");
        return;
    }

    Verbose("Encrypted String: \'" + dogApplication.Data_String_Encrypted + "\'");
    Verbose("");

    data = dogApplication.Data_String_Encrypted;
    Verbose("Decrypting String \'" + data + "\'");
    
    status = dogApplication.DecryptString(dog, data);
    ReportStatus(status);
    if (0 != status)
    {
        Verbose("");
        return;
    }
        
    Verbose("Decrypted String: \"" + dogApplication.Data_String_Decrypted + "\"");
    Verbose("");
}
    


function LoginDemo(featureId)
{
    if (window.ActiveXObject || "ActiveXObject" in window)  //IE
    {
        return LoginDemo_IE(featureId);
    }
    else
    {
        return LoginDemo_NP(featureId);
    }
}

function LoginDemo_IE(featureId)
{
    if (!dogApplication)
        return null;
    
    var feature = dogApplication.Feature(featureId);
    if (!feature)
        return null;
    
    var dog = dogApplication.Dog(feature);
    if (!dog)
        return null;
        
    var status = dog.Login();
    
    if (0 != status)
        dog = null;
    
    return dog;
}

function LoginDemo_NP(featureId)
{
    if (!dogApplication)
        return null;
    
    Verbose("Login Demo with feature " + featureId);
    Verbose("¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯");
	
    var status = dogApplication.Login(featureId);
    ReportStatus(status);
	
    if (0 != status)
        return null;
    
    Verbose("");
  
    var dog = dogApplication.Data_Handle;
    return dog;
}
    
function LoginDemoDefault()
{
    if (window.ActiveXObject || "ActiveXObject" in window)  //IE
    {
        LoginDemoDefault_IE();
    }
    else
    {
        LoginDemoDefault_NP();
    }
}

function LoginDemoDefault_IE()
{
    if (!dogApplication)
        return;

    Verbose("Login Demo with Default Feature (DefaultFeature)");
    Verbose("¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯");

    var feature = dogApplication.DefaultFeature;
    if (!feature)
    {
        Verbose("Failed to create default feature");
        return;
    }

    var dog = dogApplication.Dog(feature);
    if (!dog)
    {
        Verbose("Failed to create Dog object");
        return;
    }
    
    var status = dog.Login();
    ReportStatus(status);
    
    Verbose("");
}

function LoginDemoDefault_NP()
{
    if (!dogApplication)
        return;

    Verbose("Login Demo with Default Feature (DefaultFeature)");
    Verbose("¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯");
	
    var status = dogApplication.Login(0);
    ReportStatus(status);
    
    Verbose("");
}
    
function LogoutDemo(dog)
{
    if (window.ActiveXObject || "ActiveXObject" in window)  //IE
    {
        LogoutDemo_IE(dog);
    }
    else
    {
        LogoutDemo_NP(dog);
    }
}

function LogoutDemo_IE(dog)
{
    if (!dog)
        return;
        
    Verbose("Logout Demo");
    Verbose("¯¯¯¯¯¯¯¯¯¯¯¯");

    if (dog.IsLoggedIn())
    {
        var status = dog.Logout();
        ReportStatus(status);
    }
    
    Verbose("");
}

function LogoutDemo_NP(dog)
{
    if (!dog)
        return;
        
    Verbose("Logout Demo");
    Verbose("¯¯¯¯¯¯¯¯¯¯¯¯");

    if (dogApplication.IsLoggedIn(dog))
    {
        var status = dogApplication.Logout(dog);
        ReportStatus(status);
    }
    
    Verbose("");
}


function SessionInfoDemo_IE(dog)
{
    if (!dog)
        return;
        
    var info = dog.UpdateInfo;
    var result = dog.GetSessionInfo(info);
    var string = '';
    if (0 == result.Status)
    {
        string = result.String;
    }
    return string;
}


function ReportStatus(status)
{
    TextHistory.value += "   Result: ";
    
    var text = "Unknown status code";
    
    switch (status)
    {
        case 0:     text = "Success";
                    break;
        case 1:     text = "Request exceeds data file range";
                    break;
        case 3:     text = "System is out of memory";
                    break;
        case 4:     text = "Too many open login sessions";
                    break;
        case 5:     text = "Access denied";
                    break;
        case 7:     text = "Required SuperDog not found";
                    break;
        case 8:     text = "Encryption/decryption data length is too short";
                    break;
        case 9:     text = "Invalid input handle";
                    break;
        case 10:    text = "Specified File ID not recognized by API";
                    break;
        case 15:    text = "Invalid XML format";
                    break;
        case 18:    text = "SuperDog to be updated not found";
                    break;
        case 19:    text = "Invalid update data";
                    break;
        case 20:    text = "Update not supported by SuperDog";
                    break;
        case 21:    text = "Update counter is set incorrectly";
                    break;
        case 22:    text = "Invalid Vendor Code passed";
                    break;
        case 24:    text = "Passed time value is outside supported value range";
                    break;
        case 26:    text = "Acknowledge data requested by the update, however the ack_data input parameter is NULL";
                    break;
        case 27:    text = "Program running on a terminal server";
                    break;
        case 29:    text = "Unknown algorithm used in V2C file";
                    break;
        case 30:    text = "Signature verification failed";
                    break;
        case 31:    text = "Requested Feature not available";
                    break;
        case 33:    text = "Communication error between API and local SuperDog License Manager";
                    break;
        case 34:    text = "Vendor Code not recognized by API";
                    break;
        case 35:    text = "Invalid XML specification";
                    break;
        case 36:    text = "Invalid XML scope";
                    break;
        case 37:    text = "Too many SuperDog currently connected";
                    break;
        case 39:    text = "Session was interrupted";
                    break;
        case 41:    text = "Feature has expired";
                    break;
        case 42:    text = "SuperDog License Manager version too old";
                    break;					
        case 43:    text = "USB error occurred when communicating with a SuperDog";
                    break;
        case 45:    text = "System time has been tampered with";
                    break;
        case 46:    text = "Communication error occurred in secure channel";
                    break;
        case 50:    text = "Unable to locate a Feature matching the scope";
                    break;
        case 54:    text = "The values of the update counter in the file are lower than those in the SuperDog";
                    break;
        case 55:    text = "The first value of the update counter in the file is greater than the value in the SuperDog";
                    break;					
        case 400:   text = "Unable to locate dynamic library for API";
                    break;
        case 401:   text = "Dynamic library for API is invalid";
                    break;
        case 500:   text = "Object incorrectly initialized";
                    break;					
        case 501:   text = "Invalid function parameter";
                    break;
        case 502:   text = "Logging in twice to the same object";
                    break;
        case 503:   text = "Logging out twice of the same object";
                    break;
        case 525:   text = "Incorrect use of system or platform";
                    break;
        case 698:   text = "Requested function not implemented";
                    break;
        case 699:   text = "Internal error occurred in API";
                    break;
    }
    
    TextHistory.value += text + " (" + status + ")\n";
}

function Verbose(text)
{
    TextHistory.value += "   " + text + "\n";
    TextHistory.scrollTop = TextHistory.scrollHeight;
}